<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class artikel extends INSP_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('ctrlpnl/model_admin');
	}
	
	function index() {
		if ($this->session->userdata('logged_in') == TRUE) {
			$data['base_url'] = base_url();	
			$data['module']	 = 'artikel-admin';
			$this->load->view('ctrlpnl/vmain',$data);
		} else {
			$baseUrl = base_url();
			redirect($baseUrl.'ctrlpnl-insp-jkt-ci3500-admin-area51');
		}
	}
	
	function formdata($param=""){
		$data['editstatus'] 	= $param;
		$data['base_url'] = base_url();	
		if($param == "new"){
			$data['stateid'] 		= "";
			$data['post_title'] 	= "";
			$data['post_content'] 	= "";
			$data['category_code']	= "";
			$data['picture']		= "";
			$data['tag']			= "";
			
			//$this->load->view('ctrlpnl/artikel-admin/form-artikel', $data);
		}elseif($param == "edit" || $param == "detail"){
			$id = $this->input->post('ID');
			$baseurl = base_url();
			$query = $this->model_admin->getRecord('tb_posting', 'stateid', $id);
			
			$data['stateid'] 		= $query->stateid;
			$data['post_title'] 	= $query->post_title;
			$data['post_content'] 	= $query->post_content;
			$data['publish_status'] = ($query->publish_status == "N" ? "<font color='red'>Belum Dipublikasi</font>" : "<font color='blue'>Sudah Dipublish</font>");
			$data['status_artikel'] = $query->publish_status; 
			$data['category_code']	= $query->category_code;
			$data['pic']			= "<a href='".base_url()."assets/uploads/artikel/".$query->picture."?lightbox[width]=800&lightbox[height]=510' class='lightbox'>".(empty($query->picture) ? "" : "Gambar Utama")."</a>";
			$data['datecreate']		= date('d F Y', strtotime($query->datecreate ));
			$data['picture']		= "<a href='".base_url()."assets/uploads/artikel/".$query->picture."?lightbox[width]=800&lightbox[height]=510' class='lightbox'>".$query->picture."</a>";
			$data['tag']			= $query->tag;
			
			//
		}
		
		if($param == "detail"){
			$this->load->view('ctrlpnl/artikel-admin/detail-artikel',$data);
		}else{
			$this->load->view('ctrlpnl/artikel-admin/form-artikel', $data);
		}
	}
	
	function dataArtikel(){
		echo $this->model_admin->getData('view_posting');
	}	
	
	function saveData($param=""){
		$post = array();
		foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
				
		echo $this->model_admin->saveDataModel($post,'tb_posting',$param);

	}
	
	function deleteData(){
		$id = $this->input->post('ID');
		$folder = "artikel";
		echo $this->model_admin->deleteDataModel('tb_posting', $id, $folder);
	}
	
	function publishartikel(){
		$id 	= $this->input->post('ID');
		$status = $this->input->post('STAT');
		
		if($status == 'Y'){
			$post = array(
				'publish_status' => 'N'
			);
		}else{
			$post = array(
				'publish_status' => 'Y'
			);
		}
		
		echo $this->db->update('tb_posting', $post, array('stateid'=>$id));
	}

	function combo(){
		$val2 = $this->input->post('v');
		$rec=$this->model_admin->isi_combo('artikel');
		//print_r($rec);exit;
		$optTemp = "";
		$optTemp .= "<option value=''>-- Pilih --</option>";			
		foreach($rec as $v=>$k)
		{
			if($val2 == $k['category_code'])
				$optTemp .= "<option selected value='".$k['category_code']."'>".$k['category_name']."</option>";
			else 
				$optTemp .= "<option value='".$k['category_code']."'>".$k['category_name']."</option>";
		}
		echo $optTemp;	
	}
	
}